package ar.com.cubika.trainees.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import ar.com.cubika.trainees.bean.transportes.Chofer;
import ar.com.cubika.trainees.bean.transportes.Empresa;
import ar.com.cubika.trainees.bean.transportes.vehiculos.Camionazo;
import ar.com.cubika.trainees.bean.transportes.vehiculos.Camioncito;
import ar.com.cubika.trainees.bean.transportes.vehiculos.Camioneta;
import ar.com.cubika.trainees.bean.transportes.vehiculos.Combi;
import ar.com.cubika.trainees.bean.transportes.vehiculos.Vehiculo;
import ar.com.cubika.trainees.date.DateUtils;
import ar.com.cubika.trainees.date.RangoDeFechas;

public class VehiculoDAO extends AbstractMySQLDAO {

	// Singleton
	private static VehiculoDAO instance = new VehiculoDAO();

	private VehiculoDAO() {
	}

	public static VehiculoDAO getInstance() {
		return instance;
	}

	public Vehiculo getVehiculo(String patente) {
		
		Vehiculo veh = null;
		
		try {

			Statement stmt = getNewStatement();

			ResultSet rs = stmt.executeQuery("SELECT "
					+ "* FROM VEHICULOS WHERE PATENTE='" + patente + "';");

			while (rs.next()) {
				
				String tipo = rs.getString("tipo");
				
				if (tipo.equals("camionazo")) {
					Camionazo c = new Camionazo(patente);
					veh = c;
					
				}
				if (tipo.equals("camioneta")) {
					Camioneta c = new Camioneta(patente);
					veh = c;
				}

				if (tipo.equals("camioncito")) {
					Camioncito c = new Camioncito(patente);
					veh = c;
				}

				if (tipo.equals("combi")) {
					Combi c = new Combi(patente);
					veh = c;
				}

			}
			stmt.close();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return veh;
	}

	/**
	 * Crea un rango de fechas y retorna el ID del rango creado
	 * 
	 * @param unRango
	 * @return
	 */
	public void insertVehiculo(Vehiculo v1) {

		try {

			Statement stmt1 = getNewStatement();

			String query = "INSERT INTO VEHICULOS (PATENTE,TIPO) VALUES ('"
					+ v1.getPatente() + "','" + v1.getTipoVehiculo() + "');";

			stmt1.executeUpdate(query);

			stmt1.close();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void deleteVehiculo(String patente) {

		try {
			Statement stmt1 = getNewStatement();

			String query = "DELETE FROM VEHICULOS WHERE PATENTE ='" + patente
					+ "';";

			stmt1.executeUpdate(query);

			stmt1.close();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
public Map<String,Vehiculo> getAll() {
		
	Map<String,Vehiculo> vehiculos =  new HashMap<String,Vehiculo>(); 
		
		try {

			Statement stmt = getNewStatement();

			ResultSet rs = stmt.executeQuery("SELECT "
					+ "* FROM VEHICULOS;");

			while (rs.next()) {
				
				String tipo = rs.getString("tipo");
				String patente = rs.getString("patente");
				
				if (tipo.equals("camionazo")) {
					Camionazo c = new Camionazo(patente);
					vehiculos.put(patente, c);
					
				}
				if (tipo.equals("camioneta")) {
					Camioneta c = new Camioneta(patente);
					vehiculos.put(patente, c);
				}

				if (tipo.equals("camioncito")) {
					Camioncito c = new Camioncito(patente);
					vehiculos.put(patente, c);
				}

				if (tipo.equals("combi")) {
					Combi c = new Combi(patente);
					vehiculos.put(patente, c);
				}

			}
			stmt.close();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return vehiculos;
	}

}